<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-9-22
 * Time: 上午10:12
 */

namespace Home\Controller;
use Think\Controller;

class TablesController extends Controller {
    public function index(){
        $usercopy = M("userCopy");
        $data = $usercopy->select()->order;

        session('applylist', $data);
        $this->certStatusRedirect($target);
    }

    // 判断当前状态并跳转
    public function certStatusRedirect($target){
        // 判断状态
        if($target['in_status'] == '待审核'){
            $this->assign('user', $target);
            // 已提交申请
            $this->display('Tables/list');
        } else if($target['in_status'] == '已通过'){
            // 已通过，这块跳转需要改
            $this->assign('user', $target);
            $this->display('Tables/apply');
        } else if($target['in_status'] == null || $target['in_status'] == "未申请" ){
            // 未申请
            $this->display('Tables/certification');
        }
    }

    /**保存到文件
     * @param $usercopy
     */
    private function saveToFile($usercopy){
        //print_r($parm) ;
        //将获取的表单传过来的post值放到session中，这里面传入的是数组

        // 生日
        $_SESSION['parm']['birth_year'] = substr($usercopy['birthday'], 0, 4);
        $_SESSION['parm']['birth_month'] = substr($usercopy['birthday'], 5, 2);

        // 申请日期
        $_SESSION['parm']['apply_year'] = substr($usercopy['in_time'], 0, 4);
        $_SESSION['parm']['apply_month'] = substr($usercopy['in_time'], 5, 2);
        $_SESSION['parm']['apply_day'] = substr($usercopy['in_time'], 8, 2);

        // 硕士or本科
        if( substr($usercopy['userid'], 0, 2) == '20' ){
            $_SESSION['parm']['level'] = '硕士';
            $_SESSION['parm']['years'] = '两年半';
        } else if( substr($usercopy['userid'], 0, 2) == '30' ){
            $_SESSION['parm']['level'] = '本科';
            $_SESSION['parm']['years'] = '四年';
        }

        // 年级
        $_SESSION['parm']['grade'] = '20'.substr($usercopy['userid'], 2, 2);
        // 专业
        $_SESSION['parm']['major'] = $usercopy['major'];
        // 学院
        $_SESSION['parm']['academy'] = $usercopy['academy'];
        // 性别
        $_SESSION['parm']['sex'] = $usercopy['sex'];
        // 姓名
        $_SESSION['parm']['username'] = $usercopy['username'];
        // id
        $_SESSION['parm']['userid'] = $usercopy['userid'];
        //var_dump($_SESSION['parm']['birth_month'] );
        //执行生成doc文件的方法
        //print_r($_SESSION['parm']);
        //die();

        // 生成filepath
        $filename = $usercopy['userid'];
        $filepath = "./Public/tables/in_school_cert/target_files/".$filename.".doc";

        $this->makeDoc($filepath);
        //清空session 
        unset($_SESSION['parm']);
        return $filepath;
    }

    //创建读取、生成word文件的函数
    function makeDoc($filepath){

        if(file_exists($filepath)){
            $result=unlink ($filepath);
        }
        //打开输出缓冲区
        ob_start ();
        //在缓冲区接收test.htm文件（此文件和该代码在同一文件夹内，如果不是需要写路径）
        include("./Public/tables/in_school_cert/source.xml");
        //得到缓冲区中的内容并删除当前输出缓冲，赋值到$content中
        $content = ob_get_clean ();
        // 模板保存的时候要转成utf-8编码格式
        //echo $content;

        //打印内容到浏览器
        // print_r($content);
        //打开文件，两个变量，一个是文件名（带路径的），一个是打开模式，w+是读写方式打开，覆盖之前的文件
        $file = fopen($filepath,"w+");
        //将content中的内容写入到$file文件中
        fwrite($file,$content);
        //close the opening file
        fclose($file);
    }

    /**添加证明
     *
     */
    public function addCertAction(){

        $usercopy = M("userCopy");
        $condition['userid'] = "3010218116";
        $data = $usercopy->where($condition)->select();

        if(count($data) == 0){
            echo 'redirect,thinkphp!';
            redirect(U('Home/Login/hello'), 1, '用户不存在...');
        }

        $target = $data[0];
        $this->certStatusRedirect($target);

        $params = $_POST;
        // 判断是否存在理由
        $reason = $params['apply_reason'];
        if($reason == null || $reason == ""){
            redirect(U('Home/Tables/index'), 1, '申请理由为空：apply reason is null...');
        }
        
        $target['in_status'] = "待审核";
        $target['reason'] = $reason;
        $target['in_time'] = date('Y-m-d');
        if($target['times'] == null){
            $target['times'] = 0;
        }
        $filepath = $this->saveToFile($target);
        $target['certfilepath'] = $filepath;
        // 保存文件成功
        $usercopy->where($condition)->save($target);
        redirect(U('Home/Tables/index'), 1, '提交成功，等待审核：Successfully！Waiting for applying...');
    }

    /**
    * 获得所有的申请
    *
    */
    function getAll(){
        $usercopy = M("userCopy");
        $condition['in_status'] = "待审核";
        $alllist = $usercopy->where($condition)->order('in_time desc')->select();

        $this->assign('alllist', $alllist);
        $this->display('Tables/applylist');
    }

    /**
    * 通过申请
    *
    */
    function passApply(){
        $user = $_SESSION('usercopy');

        $user['in_status'] = "已通过";
        $this->getAll();
    }

    /**
    * 拒绝申请
    *
    */
    function refuseApply(){

    }
}